import { useHistory, useModel } from 'umi';
import ShowRentDetail from '@/components/ShowRentDetail';
import { ModelReturn, RentDetail } from '@/data-type';
import { memo, useEffect, useState } from 'react';
import { getIdFromHistory, needbackButton } from '@/utils/kits';
// import { RentDetail, ModelReturn } from '@/data-type'
const RentDetailPage: React.FC = () => {
  const model = useModel('RentDetailModel');
  // console.log(dataSource);
  // let dataSource: RentDetail[] | undefined;
  const reload = () => { return model.query({}) };
  const history = useHistory();
  const [dataSource, setDataSource] = useState<RentDetail[]>([]);
  useEffect(() => {
    if (!dataSource || dataSource.length === 0) {
      if (!needbackButton(history.location.state)) {
        model.query({}).then(data => {
          setDataSource(data as RentDetail[]);
        })
      } else {
        let id: number = getIdFromHistory(history.location.state) ?? 0;
        model.query({ programDetailId: id })
          .then(data => {
            setDataSource((data as RentDetail[]));
          })
      }
    }
  },[])
  const retModel: ModelReturn<RentDetail> = {
    ...model,
    dataSource: dataSource,
    reload: reload,
  }
  return (
    <div>
      <a onClick={() => retModel.reload()}>reload</a>
      <ShowRentDetail model={retModel} />
    </div>
  );
};

export default RentDetailPage;